/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2025-03-14 20:12:28
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2025-03-17 16:02:23
 * @FilePath: /boost_searcher/index.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <fstream>
#include <mutex>
#include "util.hpp"
#include "log.hpp"
namespace ns_index
{

    struct DocInfo
    {
        std::string title;   // 文档的标题
        std::string content; // 文档去标签之后的内容
        std::string url;     // 官网文档url
        uint64_t doc_id;     // 文档的id
    };

    struct InvertedElem
    {
        uint64_t doc_id;
        std::string word;
        int weight; // 权重
        InvertedElem()
            : weight(0)
        {
        }
    };

    // 倒排拉链
    typedef std::vector<InvertedElem> InvertedList;
    class Index
    {
    private:
        Index(){}//但是一定要有函数体，不能delete
        Index(const Index &) = delete;
        Index & operator=(const Index &) = delete;
    public:
        static Index* GetInstance()
        {   
            if(instance == nullptr)
            {
                //std::lock_guard<std::mutex> lock(mtx);
                mtx.lock();
                if(instance== nullptr)
                {
                    instance = new Index();
                }
                mtx.unlock();
            }
            return instance;
        }
        ~Index() {}
        // 根据doc_id获取文档内容
        DocInfo *GetforwardIndex(uint64_t doc_id)
        {
            if (doc_id >= forward_index.size())
            {
                std::cerr << "doc_id out of range" << std::endl;
                return nullptr;
            }
            return &forward_index[doc_id];
        }
        // 根据关键字string，获得倒排拉链
        InvertedList *GetInvertedList(const std::string &key)
        {
            auto it = inverted_index.find(key);
            if (it == inverted_index.end())
            {
                std::cerr << key << "  have no inverted_index" << std::endl;
                return nullptr;
            }
            return &(it->second);
        }
        // 根据去标签，格式化之后的文档内容，构建正排索引和倒排索引
        // data/raw_html/raw.txt
        bool BuildIndex(const std::string &input)
        {
            std::ifstream in(input, std::ios::in | std::ios::binary);
            if (!in.is_open())
            {
                std::cerr << "sorry ," << input << " open failed" << std::endl;
                return false;
            }
            std::string line;
            int count = 0;
            while (std::getline(in, line))
            {
                DocInfo *doc = BuildfowardIndex(line);
                if (doc == nullptr)
                {
                    std::cerr << "build" << line << "failed" << std::endl;
                    return false;
                }

                BuildInvertedIndex(*doc);
                count++;
                if(count%50==0)
                {
                    //std::cout << "当前已经构建的索引文档： " << count << " doc" << std::endl;
                    LOG(NORMAL,"当前已经构建的索引文档:" + std::to_string(count));
                }
            } 
            return true;
        }

    private:
        DocInfo *BuildfowardIndex(const std::string &line)
        {
            // 1.解析line，字符串切分
            // line ->3 个string：title ,  content, url
            std::vector<std::string> results;
            std::string sep = "\3";
            ns_util::StringUtil::Split(line, &results, sep); // 行内分隔符
            if (results.size() != 3)
            {
                return nullptr;
            }
            // 2. 字符串进行填充到DocIinfo
            DocInfo doc;
            doc.title = results[0];
            doc.content = results[1];
            doc.url = results[2];
            doc.doc_id = forward_index.size(); // 先进行保存id，在插入，对应的id就是当前doc在vector中的下标!
            // 3. 插入到正排索引的vector
            forward_index.push_back(std::move(doc)); // doc,html文件内容
            return &forward_index.back();
        }
        bool BuildInvertedIndex(const DocInfo &doc)
        {
            // DocInfo{title, content, url, doc_id}
            // word -> 倒排拉链
            struct word_cnt
            {
                int title_cnt;
                int content_cnt;
                word_cnt()
                    : title_cnt(0), content_cnt(0)
                {
                }
            };
            std::unordered_map<std::string, word_cnt> word_map; // 用来暂存词频的映射表
            // 对标题进行分词
            std::vector<std::string> title_words;
            ns_util::JiebaUtil::CutString(doc.title, &title_words);

            //for debug
            // if(doc.doc_id==1433)
            // {
            //     for(const auto& s : title_words)
            //     {
            //         std::cout << "title: " << s << std::endl;
            //     }
            // }

            //对标题进行词频统计
            for(auto s: title_words)
            {
                boost::to_lower(s);
                word_map[s].title_cnt++;
            }
            // 对文档内容进行分词
            std::vector<std::string> content_words;
            ns_util::JiebaUtil::CutString(doc.content, &content_words);
            // 对内容进行词频统计
            for(auto s: content_words)
            {
                boost::to_lower(s);
                word_map[s].content_cnt++;
            }

            //for debug
            // if(doc.doc_id==1433)
            // {
            //     for(const auto& s : content_words)
            //     {
            //         std::cout << "content: " << s << std::endl;
            //     }
            // }
        #define X 10
        #define Y 1
            for(const auto& word_pair : word_map)
            {
                InvertedElem item;
                item.doc_id  = doc.doc_id;
                item.word = word_pair.first;
                item.weight = word_pair.second.title_cnt*X + word_pair.second.content_cnt*Y;
                InvertedList& inverted_list = inverted_index[word_pair.first];
                inverted_list.push_back(std::move(item));
            }
        
            return true;
        }

        std::unordered_map<std::string, InvertedList> inverted_index; // 倒排索引
        std::vector<DocInfo> forward_index;                           // 正排索引 数组的下标就是天然的文档ID
        static Index* instance; 
        static std::mutex mtx;
    };
    Index* Index::instance = nullptr;
    std::mutex Index::mtx;
}